package com.ruoyi.web.controller.file;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.my.domain.MyCategory;
import com.ruoyi.my.domain.MyCategoryDetail;
import com.ruoyi.my.domain.MyModule;
import com.ruoyi.my.domain.MySearchItem;
import com.ruoyi.my.service.MyCategoryDetailService;
import com.ruoyi.my.service.MyCategoryService;
import com.ruoyi.my.service.MyModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class MyCategoryController extends BaseController {

    @Autowired
    MyCategoryService myCategoryService;
    @Autowired
    MyModuleService myModuleService;
    @Autowired
    MyCategoryDetailService myCategoryDetailService;

    /**
     * 获取文件列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('my:category:list')")
    @GetMapping("/list")
    public AjaxResult list(){
        return AjaxResult.success(myCategoryService.selectMyCategory());
    }

    @GetMapping("/searchItem")
    public AjaxResult searchItem(@RequestParam(value = "menu",required = true) String menu,@RequestParam(value = "name",required = true) String name){
        List<MyModule> myModules = myModuleService.selectMyModule(menu, name);
        Integer categoryId = -1;
//        获取categoryId
        for (MyModule myModule : myModules){
             categoryId = myModule.getCategoryId();
        }
        if (categoryId == -1){
            return AjaxResult.error(0,"没有找到该页面搜索项！");
        }
        if (categoryId == 0){
            return AjaxResult.success("该页面没有搜索项");
        }
//        搜索项类别
        MyCategory myCategory = myCategoryService.selectMyCategoryByID(categoryId);
//        搜索项内容
        List<MyCategoryDetail> categoryDetail = myCategoryDetailService.selectMyCategoryDetailByCategoryId(categoryId);
        MySearchItem mySearchItem = new MySearchItem(myCategory.getName(),categoryDetail);
        return AjaxResult.success(mySearchItem);
    }

}
